/*
 * Filename OrderController.java 2022年03月03日
 * Copyright © Ehome Co.Ltd. All Rgiths Reserved.
 * @author wuyingchao
 */
package com.ehome.poct.terminal.controller;

import cn.hutool.core.collection.CollUtil;
import com.ehome.exception.BizException;
import com.ehome.poct.core.api.dto.order.OrderDTO;
import com.ehome.poct.core.api.feign.OrderClient;
import com.ehome.poct.core.api.feign.PreOrderClient;
import com.ehome.poct.core.api.vo.order.*;
import com.ehome.poct.terminal.api.fegin.PoctClient;
import com.ehome.poct.terminal.api.fegin.TestOrderCheckRecordClient;
import com.ehome.poct.terminal.api.qry.CheckRecordQry;
import com.ehome.poct.terminal.api.vo.CheckOrderProjectVO;
import com.ehome.poct.terminal.service.CheckProjectService;
import com.ehome.poct.terminal.service.OrderService;
import com.ehome.web.annotation.RequestLog;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单
 *
 * @author wyc
 * @since 2022/03/03 13:54
 **/
@Api(tags = "检测订单")
@ApiSupport(author = "wyc")
@RestController
@AllArgsConstructor
@RequestMapping("/order")
public class OrderController {
    private final OrderService orderService;
    private final CheckProjectService projectService;
    private final OrderClient orderClient;
    private final PoctClient poctClient;
    private final PreOrderClient preOrderClient;
    private final TestOrderCheckRecordClient checkRecordClient;

    @ApiOperation("下单")
    @ApiOperationSupport(author = "wyc")
    @PostMapping("/place")
    @RequestLog
    public QrCodeVO place(@RequestBody AddOrderDTO dto) {
        // 单机检测
        if (dto.getRecordId() == null) {
            return orderService.place(dto);
        }
        // 联机检测
        return orderService.unionPlace(dto);
    }

    @ApiOperation("重新下单")
    @ApiOperationSupport(author = "wyc")
    @PostMapping("/rePlace")
    @RequestLog
    public QrCodeVO rePlace(String orderNo) {
        return orderService.rePlace(orderNo);
    }

    @ApiOperation("支付回调")
    @ApiOperationSupport(author = "wyc")
    @PostMapping("/pay/callBack")
    public void callBack() {

    }

    @ApiOperation("查询订单状态（轮询）")
    @ApiOperationSupport(author = "wyc")
    @GetMapping("/poll/status/{orderNo}")
    public PreOrderStatusVO pollGetPayStatus(@PathVariable String orderNo) {
        final var orderStatus = orderClient.pollGetStatus(orderNo);
        final var rsp = new PreOrderStatusVO().setOrderStat(orderStatus).setCheckFinish(Boolean.TRUE);
        if (orderStatus.getIsPay()) {
            final var record = checkRecordClient.query(new CheckRecordQry().setOrderNo(orderNo).setStatus(0));
            if (CollUtil.isNotEmpty(record)) {
                rsp.setCheckFinish(Boolean.FALSE).setCode(String.format("%04d", record.get(0).getReservationCode()));
            }
        }
        return rsp;
    }

    @ApiOperation("查询订单状态（主动）")
    @ApiOperationSupport(author = "wyc")
    @GetMapping("/manual/status/{orderNo}")
    public PreOrderStatusVO manualGetPayStatus(@PathVariable String orderNo) {
        final var wrapper = poctClient.getOrderStatus(orderNo);
        if (wrapper.getBusiCode() != 0) {
            throw new BizException(wrapper.getBusiCode(), wrapper.getMsg());
        }
        final var orderStatus = wrapper.getData();
        final var rsp = new PreOrderStatusVO().setOrderStat(orderStatus).setCheckFinish(Boolean.TRUE);
        if (orderStatus.getIsPay()) {
            final var record = checkRecordClient.query(new CheckRecordQry().setOrderNo(orderNo).setStatus(0));
            if (CollUtil.isNotEmpty(record)) {
                rsp.setCheckFinish(Boolean.FALSE).setCode(String.format("%04d", record.get(0).getReservationCode()));
            }
        }
        return rsp;
    }

    @ApiOperation("删除订单")
    @ApiOperationSupport(author = "wyc")
    @DeleteMapping("/delete/byOrderNo/{orderNo}")
    @RequestLog
    public void delByOrderNo(@PathVariable String orderNo) {
        orderClient.delByOrderNo(orderNo);
    }

    @ApiOperation("获取订单号对应的 订单项 价格信息")
    @GetMapping("/orderitemprice/{orderNo}")
    public List<OrderItemPriceVO> orderItemInfo(@PathVariable String orderNo) {
        return orderClient.orderItemInfo(orderNo);
    }

    @ApiOperation("支付前置-下单")
    @ApiOperationSupport(author = "wyc")
    @PostMapping("/pre/place")
    @RequestLog
    public PreOrderRspVO prePlace(@RequestBody AddPreOrderVO addVO) {
        return orderService.prePlace(addVO);
    }

    @ApiOperation("支付前置-重新下单")
    @ApiOperationSupport(author = "wyc")
    @PostMapping("/pre/rePlace/{orderNo}")
    @RequestLog
    public PreOrderRspVO preRePlace(@PathVariable String orderNo) {
        return preOrderClient.rePlace(orderNo);
    }

    @ApiOperation("支付前置-检测项目")
    @ApiOperationSupport(author = "wyc")
    @PostMapping("/pre/project/{clinicId}")
    public CheckOrderProjectVO preProject(@PathVariable Long clinicId) {
        return projectService.getCheckProjectWithGroup(clinicId);
    }

    @ApiOperation("检测订单")
    @ApiOperationSupport(author = "muqiu")
    @GetMapping("/pageOrderList")
    public CheckOrderVO pageOrderList(@SpringQueryMap OrderDTO orderDTO) {
        return orderService.pageOrderList(orderDTO);
    }

    @ApiOperation("取消订单")
    @ApiOperationSupport(author = "muqiu")
    @GetMapping("/cancelOrder/{orderNo}")
    @RequestLog
    public void cancelOrder(@PathVariable String orderNo) {
        orderService.cancelOrder(orderNo);
    }

    @ApiOperation("删除订单(逻辑删除)")
    @ApiOperationSupport(author = "muqiu")
    @DeleteMapping("/deleteOrder/{orderNo}")
    @RequestLog
    public void deleteOrder(@PathVariable String orderNo) {
        orderService.deleteOrder(orderNo);
    }

    @ApiOperation("支付前置-唤起支付")
    @ApiOperationSupport(author = "pxh")
    @PostMapping("/pre/pay")
    @RequestLog
    public PreOrderPayRspVO preRePlace(@RequestBody @Validated PreOrderPayReqDTO preOrderPayReqDTO) {
        return preOrderClient.pay(preOrderPayReqDTO);
    }

    @ApiOperation("支付前置-订单状态查询")
    @ApiOperationSupport(author = "pxh")
    @PostMapping("/pre/queryOrderStatus/{orderNo}")
    public OrderStatusVO queryOrderStatus(@PathVariable String orderNo) {
        return preOrderClient.queryOrderStatus(orderNo);
    }

}